import {
  userOrder,
  deleteOrder,
  parForOrder
} from '../../utils/api.js'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,
    orderData: {
      token: "",
      p: 1,
      state: "",
    },
    arrList: [],
    isdata: true,
    orderID:"",
    active_id:"",
    user_id:"",
    showModal: false,
    qrcodeurl: null,
  },
  //swiper切换时会调用
  pagechange: function(e) {
    if ("touch" === e.detail.source) {
      let currentPageIndex = this.data.currentIndex
      currentPageIndex = (currentPageIndex + 1) % 2
      this.setData({
        currentIndex: currentPageIndex
      })
    }
  },
  //用户点击tab时调用
  titleClick: function(e, in_state, in_currentIndex) {
    var that = this
    let state = in_state || e.currentTarget.dataset.state;
    let orderDataState = "orderData.state";
    let orderDataP = "orderData.p";
    that.setData({
      currentIndex: in_currentIndex || e.currentTarget.dataset.idx,
      [orderDataState]: state,
      [orderDataP]: 1,
      arrList: []
    })
    that.getOrder();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    let pages = getCurrentPages();
    let prevpage = pages[pages.length - 2];
    // if ( prevpage == pages/personal/personal ){

    // } else if (prevpage == pages/strategy/details/pay/pay){

    // }


    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollHeight: res.windowHeight * 2
        });
      }
    });
    wx.getStorage({
      key: 'token',
      success: function(res) {
        let orderDataToken = "orderData.token";
        that.setData({
          [orderDataToken]: res.data
        })
        that.getOrder()
      },
    })
  },
  getOrder() {
    userOrder(this.data.orderData).then(res => {
      if (res.code == 200) {
        this.setData({
          arrList: this.data.arrList.concat(res.datas.user_order)
        })
        if (this.data.orderData.state == 1 && this.data.arrList.length == 0) {
          wx.showToast({
            title: "待付款订单暂无",
            duration: 2000,
            icon: "none"
          })
        }
        if (this.data.orderData.state == 2 && this.data.arrList.length == 0) {
          wx.showToast({
            title: "待消费订单暂无",
            duration: 2000,
            icon: "none"
          })
        }
        if (this.data.orderData.state == 4 && this.data.arrList.length == 0) {
          wx.showToast({
            title: "待评价订单暂无",
            duration: 2000,
            icon: "none"
          })
        }
        if (this.data.orderData.state == 5 && this.data.arrList.length == 0) {
          wx.showToast({
            title: "完成为空",
            duration: 2000,
            icon: "none"
          })
        }
        if (res.datas.more == false) {
          this.setData({
            isdata: false
          })
        }
      } else {
        this.setData({
          arrList: []
        })
        if (this.data.orderData.state == 1 && this.data.arrList.length == 0) {
          wx.showToast({
            title: "待付款订单暂无",
            duration: 2000,
            icon: "none"
          })
        }
        if (this.data.orderData.state == 2 && this.data.arrList.length == 0) {
          wx.showToast({
            title: "待消费订单暂无",
            duration: 2000,
            icon: "none"
          })
        }
        if (this.data.orderData.state == 4 && this.data.arrList.length == 0) {
          wx.showToast({
            title: "待评价订单暂无",
            duration: 2000,
            icon: "none"
          })
        }
        if (this.data.orderData.state == 5 && this.data.arrList.length == 0) {
          wx.showToast({
            title: "完成为空",
            duration: 2000,
            icon: "none"
          })
        }
      }
      
    })
  },
  //滚动事件bindDownLoad
  bindDownLoad(e) {
    if (this.data.isdata) {
      this.data.orderData.p++
        this.getOrder()
    } else {
      wx.showToast({
        title: '到底了',
        icon: 'none'
      })
    }
  },
  discussBtn(e){
    let orderID = e.currentTarget.dataset.id.orderID;
    let user_id = e.currentTarget.dataset.id.user_id;
    let active_id = e.currentTarget.dataset.id.active_id;
    wx.navigateTo({
      url: './discuss/discuss?orderID=' + orderID + "&user_id=" + user_id + "&active_id=" + active_id,
    })
  },
  /**
   *展示核销二维码
   * */
  hexiao(e) {
    this.setData({
      showModal: true,
      qrcodeurl: e.currentTarget.dataset.qrcodeurl
    })
  },
  /**
   *关闭核销二维码
   * */
  closeModal() {
    this.setData({
      showModal: false,
      qrcodeurl: null,
    })
  },
  /**
  *删除订单
  * */
  deleteOrder(e) {
    const orderid = e.currentTarget.dataset.orderid;
    deleteOrder({id: orderid}).then(res => {
      if (res.code === 621) {
        this.setData({
          arrList: this.data.arrList.filter( i => i.orderID !== orderid),
        });
      } else {
        wx.showToast({
          title: res.msg,
          duration: 2000,
          icon: "none"
        })
      }
    });
  },
  /**
  *支付订单
  * */
  parForOrder(e) {
    const ordernum = e.currentTarget.dataset.ordernum;
    parForOrder({id: ordernum}).then(res => {
      if (res.code === 200) {
        let prepay = res.datas;
        wx.requestPayment({
          timeStamp: prepay.timestamp,
          nonceStr: prepay.nonceStr,
          package: prepay.package,
          signType: prepay.signType,
          paySign: prepay.paySign,
          success: res => {
            wx.showToast({
              title: "支付成功",
              duration: 2000,
              success: () => {
                this.titleClick(e, 2, 2)
              }
            })
          },
          fail(res) {
            wx.showToast({
              title: `支付失败：${res.msg}`,
              duration: 2000,
              success: function () {

              }
            })
          }
        })
      } else {
        wx.showToast({
          title: res.msg,
          duration: 2000,
          icon: "none"
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})